<?php 

class RecipeCommentsController extends BaseController {
    
    public function getAnswers(){
    	if( Request::ajax() ){
	    	$recipe_id = Input::get( 'recipe_id' );
	    	$parent_comment = Input::get( 'parent_comment' );
	    	
	    	$with = array( 'answersPagination.user' );
	    	
	    	$answers = RecipesComments::where( 'recipe_id', '=', $recipe_id )->where( 'parent_comment', '=', $parent_comment )
	    		->with( $with )->orderBy( 'created_at', 'desc' )->take( 10000 )->skip( RecipesComments::RECORDS_PER_PAGE_ANSWER )->get();
	    	
	    	foreach( $answers as $answer )
	    		echo View::make( 'recipe.answer', array( 'answer' => $answer ) );
	    	
	    	exit;
    	}
    }
    
    public function getComments(){
    	if( Request::ajax() ){
    		$recipe_id = Input::get( 'recipe_id' );
    		$page = Input::get( 'page' );
    		
	    	$with = array(
	    		'user',
	    		'likesDislikes',
	    		'answersCount',
    			'answersPagination',
    			'answersPagination.user',
				'answersPagination.likesDislikes' ); // Like / Dislike de las respuestas ??
	    		
    		$comments = RecipesComments::where( 'recipe_id', '=', $recipe_id )->whereNull( 'parent_comment' )
    			->with( $with )->orderBy( 'created_at', 'desc' )->take( RecipesComments::RECORDS_PER_PAGE )
    			->skip( $page * RecipesComments::RECORDS_PER_PAGE )->get();
    		
    		$userLikes = $userDislikes = array();
    		if( Auth::user() ){
    			$arrayCommentIds = array();
    			foreach( $comments as $c )
    				$arrayCommentIds[] = $c->id;
    		
    			$result = CommentsLikesDislikes::where( 'user_id', '=', Auth::user()->id )
    			->whereIn( 'comment_id', $arrayCommentIds )->get();
    		
    			foreach( $result as $r ){
    				if( $r->like == 1 )
    					$userLikes[] = $r->comment_id;
    				else
    					$userDislikes[] = $r->comment_id;
    			}
    		}
    		
    		// The recipe id it's needed. Instead of doing a query, create a std object and insert id
    		$recipe = new stdClass;
    		$recipe->id = $recipe_id;
    		
    		foreach( $comments as $comment )
    			echo View::make( 'recipe.comment', 
    					array( 'comment' => $comment, 'user' => Auth::user(), 'recipe' => $recipe, 
    							'userLikes' => $userLikes, 'userDislikes' => $userDislikes ) );
    		
    		exit;
    	}
    }
    
    public function insertAnswer(){
    	if( Request::ajax() && Auth::user() ){
    		$answer = new RecipesComments;
    		if( $answer->validate( Input::all() ) && Input::get( 'parent_comment' ) ){
    			$answer->recipe_id = Input::get( 'recipe_id' );
    			$answer->user_id = Auth::user()->id;
    			$answer->comment = Input::get( 'comment' );
    			$answer->parent_comment = Input::get( 'parent_comment' );
    			$answer->save();
    			
    			echo View::make( 'recipe.answer', array( 'answer' => $answer ) );
    			exit;
    		}else
    			return $answer->getValidationErrors()->toJSON();
    	}
    }
    
    public function insertComment(){
    	if( Request::ajax() && Auth::user() ){
    		$comment = new RecipesComments;
    		if( $comment->validate( Input::all() ) ){
    			$comment->recipe_id = Input::get( 'recipe_id' );
    			$comment->user_id = Auth::user()->id;
    			$comment->comment = Input::get( 'comment' );
    			$comment->parent_comment = NULL;
    			$comment->save();
    			
    			// The recipe id it's needed. Instead of doing a query, create a std object and insert id
    			$recipe = new stdClass;
    			$recipe->id = $comment->recipe_id;
    			
    			echo View::make( 'recipe.comment', 
    					array( 'comment' => $comment, 'user' => Auth::user(), 'recipe' => $recipe,
    							'userLikes' => array(), 'userDislikes' => array() ) );
    			exit;
    		}else
    			return $comment->getValidationErrors()->toJSON();
    	}
    }
    
    public function likeDislike(){
    	if( Request::ajax() && Auth::user() ){   		
    		// Get the inverse of like or dislike to remove it ( if exists )
    		$reverse = Input::get( 'like' ) == '1' ? 0 : 1;
    		$reverseLikeDislike = CommentsLikesDislikes::where( 'user_id', '=', Auth::user()->id )
    			->where( 'comment_id', '=', Input::get( 'comment_id' ) )
    			->where( 'recipe_id', '=', Input::get( 'recipe_id' ) )
    			->where( 'like', '=', $reverse )->first();
    		
    		if( $reverseLikeDislike ){
    			$reverseLikeDislike->delete();
    			
    			$likeDislike = new CommentsLikesDislikes;
    			$likeDislike->user_id = Auth::user()->id;
    			$likeDislike->recipe_id = Input::get( 'recipe_id' );
    			$likeDislike->comment_id = Input::get( 'comment_id' );
    			$likeDislike->like = Input::get( 'like' );
    			$likeDislike->save();
    			echo 1;
    		}else{ 
    			$likeDislike = CommentsLikesDislikes::where( 'user_id', '=', Auth::user()->id )
	    			->where( 'comment_id', '=', Input::get( 'comment_id' ) )
	    			->where( 'recipe_id', '=', Input::get( 'recipe_id' ) )
	    			->where( 'like', '=', Input::get( 'like' ) )->first();
    			
    			if( !$likeDislike ){
    				$likeDislike = new CommentsLikesDislikes;
    				$likeDislike->user_id = Auth::user()->id;
    				$likeDislike->recipe_id = Input::get( 'recipe_id' );
    				$likeDislike->comment_id = Input::get( 'comment_id' );
    				$likeDislike->like = Input::get( 'like' );
    				$likeDislike->save();
    				echo 2;
    			}else{
    				$likeDislike->delete();
    				echo 3;
    			}
    		}
    		
    		exit;
    	}
    }
}